# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Pre-commit configuration file,
# when Streamlit's pre-commit detects that one of the linters has failed,
# it automatically lints the files and does not allow the commit to pass.
# Please review the changes after lint has failed and commit them again,
# the second commit should pass,
# because the files were linted after trying to do the first commit.
repos:
  - repo: https://github.com/psf/black
    rev: 22.6.0
    hooks:
      - id: black
        args:
          # Configure Black to support only syntax supported by the minimum supported Python version in setup.py.
          - --target-version=py37
        files: \.py$|\.pyi$
        exclude: ^e2e_flaky/.*
  - repo: https://github.com/PyCQA/isort
    rev: 5.11.5
    hooks:
      - id: isort
        args:
          - --filter-files
        exclude: ^lib/streamlit/__init__\.py$
  - repo: https://github.com/PyCQA/autoflake
    rev: v1.7.7
    hooks:
      - id: autoflake
        args:
          - "--in-place"
          - "--ignore-init-module-imports"
          - "--ignore-pass-after-docstring"
          - "--remove-unused-variables"
        additional_dependencies:
          - pyflakes==3.0.1
  - repo: local
    hooks:
      # Script ./scripts/run_in_subdirectory.py was used to work around a
      # known problem with hooks in subdirectories when pass_filenames option
      # is set to true
      # See: https://github.com/pre-commit/pre-commit/issues/1417
      - id: prettier
        name: Prettier
        entry: ./scripts/run_in_subdirectory.py frontend/ yarn prettier --write
        files: ^frontend/.*\.(js|jsx|ts|tsx)$
        exclude: /vendor/
        language: node
        pass_filenames: true
      - id: prettier-yaml
        name: Prettier-yaml
        entry: ./scripts/run_in_subdirectory.py frontend yarn prettier ../.github --write
        files: ^.github/.*\.(yml|yaml)$
        language: node
        pass_filenames: false
      # Typecheck will be run on CI using --hook-stage manual flag,
      # check out .github/workflows/js-tests.yml "Run linters" step for details.
      - id: typecheck-app
        name: Typecheck App
        always_run: true
        entry: yarn --cwd frontend/app tsc --noEmit
        files: \.(js|jsx|ts|tsx)$
        language: node
        pass_filenames: false
        stages:
          - manual
      - id: typecheck-lib
        name: Typecheck Lib
        always_run: true
        # no noEmit flag bc tsconfig.json emitDeclaration key is used
        entry: yarn --cwd frontend/lib tsc
        files: \.(js|jsx|ts|tsx)$
        language: node
        pass_filenames: false
        stages:
          - manual
      # Eslint will be run on CI using Makefile,
      # check out .github/workflows/js-tests.yml "Run linters" step for details.
      - id: eslint
        name: Eslint
        always_run: true
        entry: ./scripts/run_in_subdirectory.py frontend/ yarn lint --fix
        files: ^frontend/src/.*\.(js|jsx|ts|tsx)$
        language: node
        pass_filenames: true
        stages:
          - manual
      - id: license-headers
        name: Checks that all files have the required license headers
        entry: ./scripts/check_license_headers.py
        language: system
        always_run: true
        pass_filenames: false
      - id: no-relative-imports
        language: pygrep
        name: No relative imports
        description: Streamlit style is to use absolute imports only (except docs building)
        entry: "^\\s*from\\s+\\."
        pass_filenames: true
        files: \.py$
  - repo: https://github.com/Lucas-C/pre-commit-hooks
    rev: v1.5.4
    hooks:
      - id: insert-license
        name: Add license for all (S)CSS/JS(X)/TS(X) files
        files: \.(s?css|jsx?|tsx?)$
        args:
          - --comment-style
          - "/**| *| */"
          - --license-filepath
          - scripts/license-template.txt
          - --fuzzy-match-generates-todo
        exclude: |
          (?x)
          /vendor/
          |^vendor/
          |^component-lib/declarations/apache-arrow
          |^frontend/app/src/assets/css/variables\.scss
      - id: insert-license
        name: Add license for all Proto files
        files: \.proto$
        args:
          - --comment-style
          - "/**!| *| */"
          - --license-filepath
          - scripts/license-template.txt
          - --fuzzy-match-generates-todo
        exclude: |
          (?x)
          /vendor/
          |^vendor/
          |^component-lib/declarations/apache-arrow
          |^proto/streamlit/proto/openmetrics_data_model\.proto
      - id: insert-license
        name: Add license for all shell files
        files: \.sh$
        args:
          - --comment-style
          - "|#|"
          - --license-filepath
          - scripts/license-template.txt
          - --fuzzy-match-generates-todo
        exclude: |
          (?x)
          /vendor/
          |^vendor/
          |^component-lib/declarations/apache-arrow
      - id: insert-license
        name: Add license for all Python files
        files: \.py$|\.pyi$
        args:
          - --comment-style
          - "|#|"
          - --license-filepath
          - scripts/license-template.txt
          - --fuzzy-match-generates-todo
        exclude: |
          (?x)
          /vendor/
          |^vendor/
          |^component-lib/declarations/apache-arrow
          |^lib/tests/isolated_asyncio_test_case\.py$
          |^e2e_flaky/.*
      - id: insert-license
        name: Add license for all HTML files
        files: \.html$
        args:
          - --comment-style
          - "<!--||-->"
          - --license-filepath
          - scripts/license-template.txt
          - --fuzzy-match-generates-todo
        exclude: |
          (?x)
          /vendor/
          |^vendor/
          |^component-lib/declarations/apache-arrow
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.3.0
    hooks:
      - id: trailing-whitespace
        exclude: |
          (?x)
          ^frontend/app/src/assets/
          |^NOTICES$
          |^proto/streamlit/proto/openmetrics_data_model.proto$
          |\.snap$
